/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License.
 */

package au.com.uptick.serendipity.server.handler;

import au.com.uptick.serendipity.server.domain.Account;
import au.com.uptick.serendipity.server.domain.dao.AccountDao;
import au.com.uptick.serendipity.shared.action.RetrieveAccountsAction;
import au.com.uptick.serendipity.shared.action.RetrieveAccountsResult;
import au.com.uptick.serendipity.shared.dto.AccountsDto;
import com.allen_sauer.gwt.log.client.Log;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;
import org.apache.log4j.xml.DOMConfigurator;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class RetrieveAccountsHandler implements ActionHandler<RetrieveAccountsAction, RetrieveAccountsResult> {

    // static Logger logger = Logger.getLogger(RetrieveAccountsHandler.class);

    // private final Log logger;
    private final Provider<HttpServletRequest> requestProvider;
    private final ServletContext servletContext;

    @Inject
    RetrieveAccountsHandler(final ServletContext servletContext,
                            final Provider<HttpServletRequest> requestProvider) {
        // this.logger = logger;
        this.servletContext = servletContext;
        this.requestProvider = requestProvider;
    }

    @Override
    public RetrieveAccountsResult execute(RetrieveAccountsAction action,
                                          ExecutionContext context) throws ActionException {

        RetrieveAccountsResult result = null;
        AccountDao accountDao = new AccountDao();

        DOMConfigurator.configure("log4j.xml");

        // logger.info("Retrieve Accounts");
        Log.info("Retrieve Accounts");

        try {
            List<Account> accounts = accountDao.retrieveAccounts();

            if (accounts != null) {
                List<AccountsDto> accountsDtos = new ArrayList<AccountsDto>(accounts.size());

                for (Account account : accounts) {
                    accountsDtos.add(createAccountsDto(account));
                }

                result = new RetrieveAccountsResult(accountsDtos);
            }
        } catch (Exception e) {
            // logger.warn("Unable to retrieve Accounts - ", e);
            Log.warn("Unable to retrieve Accounts - ", e);

            throw new ActionException(e);
        }
        /*
        catch (Throwable t) {
          t.printStackTrace();
        }
        */

        return result;
    }

    private AccountsDto createAccountsDto(Account account) {
        return new AccountsDto(account.getAccountId(), account.getAccountName(), account.getMainPhone(),
                               account.getCity(), account.getPrimaryContact(), account.getEmailPrimaryContact());
    }

    @Override
    public Class<RetrieveAccountsAction> getActionType() {
        return RetrieveAccountsAction.class;
    }

    @Override
    public void undo(RetrieveAccountsAction action, RetrieveAccountsResult result,
                     ExecutionContext context) throws ActionException {
    }
}